﻿using System;
using System.Globalization;
using System.Linq;

namespace Borg.Framework
{
    public static class MenuItemParser
    {
        public static MenuItemPath ParseMenuItemAttributes(string menuItemString)
        {
            var path = new MenuItemPath();

            var dblPound = menuItemString.Split(new[] { "##" }, StringSplitOptions.None);
            var tilde = dblPound[0].Split(new[] { '~' });

            foreach (var pipe in tilde.Select(
                str => str.Split(new[] { '|' })).Where(strArray3 => strArray3.Length != 0))
            {
                double maxValue;

                if (!(pipe.Length != 1 &&
                      double.TryParse(pipe[1], NumberStyles.Float, CultureInfo.InvariantCulture, out maxValue)))
                    maxValue = double.MaxValue;

                path.Add(new MenuItemAttributes(pipe[0], maxValue));
            }

            if (dblPound.Length > 1)
                path.Icon = dblPound[1];

            return path;
        }
    }
}

